package com.fhd.service.Merchant;

import com.fhd.dao.BaseDao;
import com.fhd.dao.Merchant.MerchantDao;
import com.fhd.dao.Merchant.MerchantDaoImplement;
import com.fhd.pojo.Client;
import com.fhd.pojo.Merchant;
import com.fhd.pojo.Recharge;

import java.sql.Connection;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Objects;

public class MerchantServiceImplement implements MerchantService{
    // 业务层调用Dao层
    private MerchantDao merchantDao = null;
    public MerchantServiceImplement() {
        merchantDao = new MerchantDaoImplement();
    }

    // 商户登录
    @Override
    public Merchant login(String userCode, String userPassword) {
        Connection conn = null;
        Merchant merchant = null;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            merchant = merchantDao.getLoginMerchant(conn,userCode);

            //判断找到的商户不为空的前提下密码是否对
            if (merchant != null) {
                if (!Objects.equals(merchant.getUserPassword(),userPassword)) {
                    merchant = null;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return merchant;
    }

    // 通过id得到商户
    @Override
    public Merchant getMerchantById(int id) {
        Connection conn = null;
        Merchant merchant = null;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            merchant = merchantDao.getMerchantById(conn,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return merchant;
    }

    //商户注册
    @Override
    public int signup(String userCode, String userPassword, String userName, String shopName, String address, String phone, String keyWord, Time workTimeBegin, Time workTimeEnd) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            affectedRow = merchantDao.signUpMerchant(conn,userCode,userPassword,userName,shopName,address,phone,keyWord,workTimeBegin,workTimeEnd);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 得到所有商户
    @Override
    public ArrayList<Merchant> getAllMerchant() {
        Connection conn = null;
        ArrayList<Merchant> merchants = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            merchants = merchantDao.getAllMerchant(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return merchants;
    }

    // 搜索商户
    @Override
    public ArrayList<Merchant> getSomeMerchant(String shopName) {
        Connection conn = null;
        ArrayList<Merchant> merchants = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            merchants = merchantDao.getSomeMerchant(conn,shopName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return merchants;
    }

    // 改变申请状态
    @Override
    public int changeApplyMerchant(int id, int situation) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            affectedRow = merchantDao.changeAppleSituation(conn,id,situation);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 更改商户信息
    @Override
    public int updateInformation(int id, String userCode, String userPassword, String userName, String shopName, String address, String phone, String keyWord, Time workTimeBegin, Time workTimeEnd, int openSituation) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            affectedRow = merchantDao.updateInformation(conn,id,userCode,userPassword,userName,shopName,address,phone,keyWord,workTimeBegin,workTimeEnd,openSituation);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 排序商户
    @Override
    public ArrayList<Merchant> orderMerchant(String soldNumberOrder, String createDateOrder) {
        Connection conn = null;
        ArrayList<Merchant> merchants = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            merchants = merchantDao.orderMerchant(conn,soldNumberOrder,createDateOrder);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return merchants;
    }


}
